package org.mch.main;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.mch.main.panel.MFuncTreeModel;

public class MDao {

	private QueryRunner qr = new QueryRunner();

	protected Connection getConn() throws Exception {
		URL url = MFuncTreeModel.class.getResource("/resource/ddata/mchutils.db");
		Class.forName("org.sqlite.JDBC");
		Connection conn = DriverManager.getConnection("jdbc:sqlite:" + url.getFile());
		return conn;
	}

	public <T> T query(String sql, ResultSetHandler<T> rsh, Object... params) throws Exception {
		return getQr().query(getConn(), sql, rsh, params);
	}

	public <T> T query(String sql, ResultSetHandler<T> rsh) throws Exception {
		return getQr().query(getConn(), sql, rsh);
	}

	public int update(String sql) throws Exception {
		return getQr().update(getConn(), sql);
	}

	public int update(String sql, Object param) throws Exception {
		return getQr().update(getConn(), sql, new Object[] { param });
	}

	public int update(String sql, Object... params) throws Exception {
		return getQr().update(getConn(), sql, params);
	}

	public QueryRunner getQr() {
		return qr;
	}
}
